package com.sc.nft.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.sc.nft.annotation.UserLogin;
import com.sc.nft.annotation.WithoutSign;
import com.sc.nft.controller.request.GetAppVersionRequest;
import com.sc.nft.dao.StaticDataDao;
import com.sc.nft.entity.AppVersion;
import com.sc.nft.entity.StaticData;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.vo.AppVersionVO;
import com.sc.nft.entity.vo.DwonPageVO;
import com.sc.nft.enums.AppTypeEnum;
import com.sc.nft.enums.MessageTopic;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.helper.MessageQueueHelper;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.AppVersionService;
import com.sc.nft.sup.ErrorCode;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 服务控制器
 *
 * @author Sun
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2022-05-29 17:37:58
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/app")
@Api(tags = "APP版本")
public class AppVersionController {

    private final AppVersionService appVersionService;
    private final MessageQueueHelper MessageQueueHelper;
    private final StaticDataDao staticDataDao;

    private final List<String> versionType = Lists.newArrayList(AppTypeEnum.IOS.getName(),AppTypeEnum.ANDROID.getName(),AppTypeEnum.ANDROID_MERCHANT.getName());

    @ApiOperation(value = "获取版本信息", notes = "获取版本信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getVersion", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin(required = false)
    @WithoutSign
    public Result<AppVersionVO> getAppVersion(@Valid @RequestBody GetAppVersionRequest request) {

        String type = request.getType();

        if (!versionType.contains(type)) {
            throw new GlobalRunTimeException(ErrorCode.PARAM_ERROR);
        }
        UserInfo user = UserPool.getUser();
        AppVersion appVersion = appVersionService.findOneByType(type);
        Boolean isReview = Boolean.FALSE;
        if(type.equals("ios")){
            String appStoreVersion = request.getAppStoreVersion();
            if(StrUtil.isNotBlank(appStoreVersion) && appVersion.getAppStoreVersion().equals(appStoreVersion)){
                isReview =Boolean.TRUE;
            }else if(ObjectUtil.isNotNull(user)){
                String userTel = user.getUserTel();
                if (userTel.equals("13849904109") || userTel.equals("16725719559")) {
                    isReview =Boolean.TRUE;
                }
            }
            AppVersionVO appVersionVO = BeanUtil.toBean(appVersion, AppVersionVO.class);
            appVersionVO.setIsReview(isReview);
            return Result.success_ios(appVersionVO);
        }
        return Result.success(appVersion, AppVersionVO.class);

    }

    @ApiOperation(value = "获取下载的URL页面地址", notes = "获取下载的URL页面地址", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getDwonPage", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<DwonPageVO> getDwonPage() {
        StaticData staticData = staticDataDao.getByType("down_page");
        return Result.success(staticData.getValue(), DwonPageVO.class);

    }



}


